//
//  XHQBaseScrollerVC.m
//  西楚网
//
//  Created by 徐汉卿 on 16/7/6.
//  Copyright © 2016年 徐汉卿. All rights reserved.
//

#import "XHQBaseScrollerVC.h"
static CGFloat zheDangHeight;

@interface XHQBaseScrollerVC ()

@end

@implementation XHQBaseScrollerVC

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    if (self.sv.contentSize.height <= self.sv.height ) {
        [self.sv setContentSize:CGSizeMake(0, self.sv.height + 1)];
    }

    // 监听键盘的即将显示事件. UIKeyboardWillShowNotification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    // 监听键盘即将消失的事件. UIKeyboardWillHideNotification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    
}




-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    NSLog(@"viewWillDisappear    %@   %f",self,self.sv.contentOffset.y);

    [NSDC removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    [self buildUI];
}
//交给子类实现  防止子类没写崩溃,在此类写个空发发
-(void)buildUI{
    
}

-(XHQBaseScrollView *)sv
{
    if (_sv == nil) {
        _sv = [XHQBaseScrollView new];
        _sv.delegate  =self;
        _sv.bounces = YES;
//        _sv.contentInset =  UIEdgeInsetsMake(0, 0, 216, 0);
        //解决 sv  再次出现 滚动位置变化
        

        
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shouJianPan)];

        [_sv addGestureRecognizer:tap];

        
        
        [self.view addSubview:_sv];
        [_sv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);

        }];
    }
    return _sv;
}






-(void)shouJianPan
{
    
    [UIView animateWithDuration:0.2 animations:^{
//        _sv.contentOffset  =CGPointZero;
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    }];
    
    
    
}














#pragma mark -- 代理

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"xxxxx%f",scrollView.contentOffset.y);
    
    [self shouJianPan];
}



- (void) keyboardWillShow:(NSNotification *)notify {
    //这里只写了关键代码，细节根据自己的情况来定，sv为弹出键盘的视图，UITextField
    CGFloat kbHeight = [[notify.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;//获取键盘高度，在不同设备上，以及中英文下是不同的，很多网友的解决方案都把它定死了。
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
    
    CGRect TF_In_windowFrame  = [firstResponder convertRect:firstResponder.frame toView:keyWindow];
    
    
    if (TF_In_windowFrame.origin.y + TF_In_windowFrame.size.height > (keyWindow.frame.size.height - kbHeight) ) {
        NSLog(@"有遮挡");
        zheDangHeight =  TF_In_windowFrame.origin.y + TF_In_windowFrame.size.height - (keyWindow.frame.size.height - kbHeight);
        

        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake( self.view.x, self.view.y - zheDangHeight, self.view.width, self.view.height);
        }];
        
        
    }else{
        NSLog(@"没有遮挡");
        zheDangHeight = 0.0;
    }
    
    
    
}
- (void) keyboardWillHidden:(NSNotification *)notify {//键盘消失
    // 键盘动画时间
     double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    
    [UIView animateWithDuration:duration animations:^{
         self.view.frame = CGRectMake( self.view.x, self.view.y + zheDangHeight, self.view.width, self.view.height);
    }];
   
}


@end
